package com.example.smartcity.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity.R
import com.example.smartcity.http.Seat

/**
 * @Author: Tailyou
 * @CreateDate: 2021/10/7 19:11
 * @Email: tailyou@163.com
 * @Description: RecycleView数据适配器（新闻列表）
 */
class SeatListAdapter(private var datas: List<Seat>, var listener: OnItemClickListener) :
    RecyclerView.Adapter<SeatListAdapter.MViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MViewHolder {
        var view = LayoutInflater.from(parent.context)
            .inflate(R.layout.layout_item_clo_seat, parent, false)
        return MViewHolder(view)
    }

    override fun onBindViewHolder(holder: MViewHolder, position: Int) {
        //需要注意，平时写RecyclerView的Adapter，一行一条数据；
        //这里一行需要用到6条数据，这6条座位数据的clo一样
        val seatA = datas[position * 6 + 0]
        val seatB = datas[position * 6 + 1]
        val seatC = datas[position * 6 + 2]
        val seatJ = datas[position * 6 + 3]
        val seatK = datas[position * 6 + 4]
        val seatL = datas[position * 6 + 5]
        //用这6条数据，来处理这一行显示的内容
        holder.tvClo.text = seatA.clo
        holder.ivSeatA.setImageResource(if (seatA.state == 2) R.mipmap.seat_full else R.mipmap.seat_empty)
        holder.ivSeatA.setOnClickListener { listener.onClick(seatA) }
        holder.ivSeatB.setImageResource(if (seatB.state == 2) R.mipmap.seat_full else R.mipmap.seat_empty)
        holder.ivSeatB.setOnClickListener { listener.onClick(seatB) }
        holder.ivSeatC.setImageResource(if (seatC.state == 2) R.mipmap.seat_full else R.mipmap.seat_empty)
        holder.ivSeatC.setOnClickListener { listener.onClick(seatC) }
        holder.ivSeatJ.setImageResource(if (seatJ.state == 2) R.mipmap.seat_full else R.mipmap.seat_empty)
        holder.ivSeatJ.setOnClickListener { listener.onClick(seatJ) }
        holder.ivSeatK.setImageResource(if (seatK.state == 2) R.mipmap.seat_full else R.mipmap.seat_empty)
        holder.ivSeatK.setOnClickListener { listener.onClick(seatK) }
        holder.ivSeatL.setImageResource(if (seatL.state == 2) R.mipmap.seat_full else R.mipmap.seat_empty)
        holder.ivSeatL.setOnClickListener { listener.onClick(seatL) }
    }

    override fun getItemCount(): Int {
        return datas.size / 6
    }

    class MViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivSeatA: ImageView = itemView.findViewById(R.id.ivSeatA)
        var ivSeatB: ImageView = itemView.findViewById(R.id.ivSeatB)
        var ivSeatC: ImageView = itemView.findViewById(R.id.ivSeatC)
        var ivSeatJ: ImageView = itemView.findViewById(R.id.ivSeatJ)
        var ivSeatK: ImageView = itemView.findViewById(R.id.ivSeatK)
        var ivSeatL: ImageView = itemView.findViewById(R.id.ivSeatL)
        var tvClo: TextView = itemView.findViewById(R.id.tvClo)
    }

    interface OnItemClickListener {
        fun onClick(seat: Seat)
    }

}